<template>
<div>
      <div class="header">
        <div class="container">
           <div class="left">km数码商城</div>
        <div class="right">
          <span>账号：{{userInfo.account}} </span>
          <span ><router-link class="go" to="/userlogin">切换账号</router-link></span>
          <span @click="gomyorder" class="go"> 我的订单</span>  
          <span ><router-link class="go" to="/"> 后台管理</router-link></span>
          </div> 
        </div>
      </div>
      <div class="phone">
         <div class="container">
           <div class="phone-main">
            <div class="title">
               <h2>商品</h2>
            </div>
            <div class="content">
               <div class="content-right">
                 <ul>
                  <li v-for="item in itemsList" :key="item.id">

                    <!-- 第一种模板字符串 -->
                    <!-- <img :src="require(`@/assets/${item.imgurl}.png`)"> --> 
                    <!-- 第二种字符串拼接 -->
                    <img :src="require('@/assets/'+item.imgurl)"> 

                    <p>{{item.itemsname}}</p>
                    <p>{{item.amount}}元起</p>
                    <a href="javascript:;" @click="buy(item)">购买</a>
                    <el-popover placement="top-start" title="商品描述" :width="200" trigger="hover">
                      <template #reference>
                        <div>商品描述</div> 
                      </template>
                       <p >{{item.description}}</p>
                  </el-popover>

                   </li>
                 </ul>
               </div>
            </div>
           </div>
         </div>
         <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.current"
             :page-size="queryInfo.pagesize" layout="total, prev, pager, next, jumper" :total="total">
        </el-pagination>          
        </div>
      <div class="footer"></div>
      <el-dialog v-model="addDialogVisible" title="收货信息" width="50%" center>
                <p>商品名称：{{itemsForm.items}}</p>
                <p>商品价格：{{itemsForm.amount}}</p>   
        <el-form :model="itemsForm" label-width="120px">
                <el-form-item label="收货人">
                <el-input v-model="itemsForm.username" />
                </el-form-item>
                <el-form-item label="联系电话">
                <el-input v-model="itemsForm.phone" />
                </el-form-item>
                <el-form-item label="商品数量">
                <el-input type="number" v-model="itemsForm.number" max="3" min="1" />
                </el-form-item>
                <el-form-item label="收货地址">
                <el-input v-model="itemsForm.address" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="saveBuy">确定</el-button>
                </span>
            </template>
        </el-dialog>
</div>
</template>
<script>
    export default{
        name:'Shop',
        data(){
            return{
                itemsList:[],
                queryInfo:{
                queryId:'',
                current:1,
                pagesize:10
                },
                total:'',
                userInfo:'',       
                itemsForm:{
                    username:'',
                    items:'',
                    amount:'',
                    number:'',
                    phone:'',
                    address:'',
                    uid:''
                },
                addDialogVisible:false,    
            }

        },
        created(){
          this.getItemsList()
        },
        mounted(){
            
            this.getUserListById()   

        },
        methods:{
            async getItemsList(){
                const result=await this.$http.get('/items/find',{params:{i:this.queryInfo.current,j:this.queryInfo.pagesize}})
                this.itemsList=result.records
                this.total=result.total
                console.log(this.itemsList);
                
            },
            //分页变化触发
            handleCurrentChange (val)  {
            this.queryInfo.current=val
            this.getItemsList()
            },
            // 根据Id查询用户
            async getUserListById(){
            const result=  await this.$http.get('/users',{params:{id:this.$route.params.id}})
            // console.log(result[0]);
            this.userInfo=result[0]
            Object.keys(this.itemsForm).forEach(key=>{this.itemsForm[key]=this.userInfo[key]}) 
            this.itemsForm.uid=this.userInfo.id
            // console.log(this.itemsForm);  
            
            }, 
            //点击购买           
            buy(item){
              this.addDialogVisible=true
              this.itemsForm.items=item.itemsname
              this.itemsForm.amount=item.amount
              this.itemsForm.number=1
            },
            //确认购买
            async saveBuy(){
                const result=await this.$http.post('/order/add',this.itemsForm)
                console.log(result);
                if(result.code==200)
                
                this.$router.push(`/userorder/${this.userInfo.id}`)   
            },
            gomyorder(){
              this.$router.push(`/userorder/${this.userInfo.id}`) 
            },

        }
    }
</script>
<style scoped>
.el-pagination{
        margin-left: 400px;
    }
.el-dialog p{
    text-align: center;
    color: red;
}
.container {
  margin: 0 auto;
  width: 1226px;
}
.header{
    width: 100%;
    height: 50px;
    background-color:#333;
    line-height: 50px;
    color:#b0b0b0;
}
.header .left{
    float: left;
    font-size: 18px;
}
.header .right{
    font-size: 14px;
    float: right;
}
.header .right span{
  margin-left: 10px;
}
.header .right .go:hover{
  color: #fff;
}
.header .right a{
  color: #b0b0b0;
}

.phone {
  background-color: #f5f5f5;
  width: 100%;
  padding-top: 4px;
  padding-bottom: 12px;
}
.phone .phone-main .title {
  height: 58px;
  line-height: 58px;
}
.phone .phone-main .title h2 {
  float: left;
  font-size: 22px;
  font-weight: 200;
}

.phone .phone-main .content {
  height: 614px;
}
.phone .phone-main .content .content-right {
  float: left;
  width: 1248px;
}
.phone .phone-main .content .content-right li {
  float: left;
  width: 234px;
  height: 300px;
  margin-left: 14px;
  margin-bottom: 14px;
  padding-top: 40px;
  text-align: center;
  background-color: #fff;
}
.phone .phone-main .content .content-right li img {
  height: 160px;
}
.phone .phone-main .content .content-right li p{
  font-size: 14px;
}
.phone .phone-main .content .content-right li a{
    color: blue;
}
.phone .phone-main .content li:hover {
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  transform: translate3d(0, -2px, 0);
}
</style>